package Entities;

import digitshoplogic.DBAccessSingleton;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Employee {
	public int employeeId;
	public String name;
	public String password;
	public Integer type;
        
       public static final Employee getEmployeeByemployeeId(int employeeId) throws SQLException {
        Employee employee1 = new Employee();

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("select * from employee where employee.employeeId = ?");
         ps.setInt(1, employeeId);

        ResultSet rs = ps.executeQuery();

         while (rs.next()) {
                employee1.employeeId = rs.getInt(1);               
                employee1.name = rs.getString(2);
                employee1.password = rs.getString(3);
                employee1.type = rs.getInt(4);
            } 
            rs.close();
            return employee1;
        }
public static final void addEmployeeByObj(Employee em) throws SQLException {

         PreparedStatement ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("INSERT INTO employee VALUES (?,?,?,?)");

         ps.setInt(1, em.employeeId);
         ps.setString(2, em.name);
         ps.setString(3, em.password);
         ps.setInt(4, em.type);     

         ps.executeUpdate();
         ps.close();
        }
 public static final void deleteEmployeeByID(int id) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("DELETE FROM employee WHERE employee.employeeId= ?");
	 ps.setInt(1, id);
         ps.executeUpdate();
         ps.close();
     }

  public static final void updateEmployeeByObj(Employee em) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("UPDATE employee SET "
                 + "employee.name = ?, "
                 + "employee.password = ?, "
                 + "employee.type = ? WHERE employee.employeeId = ?");

	 ps.setInt(4, em.employeeId);
         ps.setString(1, em.name);
         ps.setString(2, em.password);
         ps.setInt(3, em.type);

         ps.executeUpdate();
         ps.close();

     }


    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

}